package com.rtx.wms.std.handler.so.upload;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.edi.model.api.bo.ApiMappingInfoBo;
import com.rtx.wms.customer.common.upload.UploadService;
import com.rtx.wms.std.common.bean.WmsConstant;
import com.rtx.wms.std.so.bean.ApiOrdersfinish;
import com.rtx.wms.std.so.bean.Orders;
import com.rtx.wms.std.so.service.ApiOrdersfinishService;
import com.rtx.wms.std.so.service.OrdersService;

import cn.hutool.core.util.StrUtil;

/**
 * 出库单回传
 */
@Component
public class OrdersReturnHandler{

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ApiOrdersfinishService apiOrdersfinishService;

    @Autowired
    OrdersService ordersService;
    
    @Autowired
    ApplicationContext applicationContext;

    /**
     * 出库单回传
     * @param whseid
     */
    public void handlerOrdersReturn(String whseid,ApiMappingInfoBo bo){
        //获取出库单回传数据
        List<ApiOrdersfinish> ordersList = selectApiOrdersfinish("ris_edi");
       
        for(ApiOrdersfinish o : ordersList){
            //回传
        	UploadService service =(UploadService)applicationContext.getBean(bo.getApimappingcode());
        	service.uploadHandle(o);
            //更新状态
            updateApiOrdersfinish("ris_edi",o);
        }
    }
    

    /**
     * 更新
     * @param whseid
     * @param o
     */
    protected void updateApiOrdersfinish(String whseid, ApiOrdersfinish o) {
        try {
            LambdaUpdateWrapper<ApiOrdersfinish> wrapper = Wrappers.lambdaUpdate();
            wrapper.eq(ApiOrdersfinish::getSerialkey,o.getSerialkey())
                    .set(ApiOrdersfinish::getEdiflag,o.getEdiflag())
                    .set(ApiOrdersfinish::getCallshipediflag, o.getCallshipediflag())
                    .set(ApiOrdersfinish::getEdimsg, StrUtil.sub(o.getEdimsg(),0,200))
                    .set(ApiOrdersfinish::getRtxqueue,o.getRtxqueue() + 1);
            apiOrdersfinishService.updateByWrapper(whseid,wrapper);
            
            
            LambdaUpdateWrapper<Orders> owrapper = Wrappers.lambdaUpdate();
            owrapper
            .eq(Orders::getOrderkey, o.getOrderkey())
            .set(Orders::getOrderfinishflag, o.getEdiflag())
            .set(Orders::getReturnfailedreason,StrUtil.sub(o.getEdimsg(),0,200));
            ordersService.updateByWrapper(o.getWhseid(), owrapper);
        } catch (Exception e) {
            logger.error("出库单：{}更新回传状态异常",o.getOrderkey(),e);
        }
    }

    /**
     * 获取出库单回传数据
     * @param whseid
     * @return
     */
    protected List<ApiOrdersfinish> selectApiOrdersfinish(String whseid){
        LambdaQueryWrapper<ApiOrdersfinish> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(ApiOrdersfinish::getEdiflag, Arrays.asList(WmsConstant.DEFAULT_FLAG,WmsConstant.FAIL_FLAG))
                    .lt(ApiOrdersfinish::getRtxqueue,WmsConstant.DEFAULT_COUNT);
//        queryWrapper.eq(ApiOrdersfinish::getExternorderkey, "100059");
        return apiOrdersfinishService.selectApiOrdersfinishByWrapper(whseid,queryWrapper);
    }
}
